﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.SupportCMS.Models.Object;
using Guline.SupportCMS.Models.Context;

namespace Guline.SupportCMS.Controllers.Manage
{
    public class TemplateHookController : GulineBaseController
    {


        //
        // GET: /TemplateHook/
        public ActionResult jsonList(int ParentID)
        {
            var Hooks = from m in db.TemplateHooks where m.ParentID == ParentID select new { m.ID, m.Name, m.ItemID, m.Order, m.Enable, StyleName = m.Style.Name };
            return Json(Hooks, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {

            return View();
        }

        //
        // GET: /TemplateHook/Details/5

        public ActionResult Details(int id = 0)
        {
            TemplateHook templatehook = db.TemplateHooks.Find(id);
            if (templatehook == null)
            {
                return HttpNotFound();
            }
            return View(templatehook);
        }

        //
        // GET: /TemplateHook/Create

        public ActionResult Create(int ParentID)
        {
            ViewBag.TemplateID = new SelectList(db.Templates, "ID", "Name");
            ViewBag.StyleID = new SelectList(db.Styles, "ID", "Name");
            ViewBag.ParentID = new SelectList(db.TemplateHooks, "ID", "Name", ParentID);
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View();
        }

        //
        // POST: /TemplateHook/Create
        [ValidateInput(false)]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TemplateHook templatehook)
        {
            if (ModelState.IsValid)
            {
                templatehook.Content = HttpUtility.HtmlEncode(templatehook.Content);
                db.TemplateHooks.Add(templatehook);
                db.SaveChanges();
                return RedirectToAction("Index", new { ParentID = templatehook.ParentID });
            }
            ViewBag.TemplateID = new SelectList(db.Templates, "ID", "Name", templatehook.TemplateID);
            ViewBag.StyleID = new SelectList(db.Styles, "ID", "Name", templatehook.StyleID);
            ViewBag.ParentID = new SelectList(db.TemplateHooks, "ID", "Name", templatehook.ParentID);
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View(templatehook);
        }

        //
        // GET: /TemplateHook/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TemplateHook templatehook = db.TemplateHooks.Find(id);
            if (templatehook == null)
            {
                return HttpNotFound();
            }
            templatehook.Content = HttpUtility.HtmlDecode(templatehook.Content);

            ViewBag.TemplateID = new SelectList(db.Templates, "ID", "Name", templatehook.TemplateID);
            ViewBag.StyleID = new SelectList(db.Styles, "ID", "Name", templatehook.StyleID);
            ViewBag.ParentID = new SelectList(db.TemplateHooks, "ID", "Name", templatehook.ParentID);
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View(templatehook);
        }

        //
        // POST: /TemplateHook/Edit/5

        [ValidateInput(false)]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TemplateHook templatehook)
        {
            if (ModelState.IsValid)
            {
                templatehook.Content = HttpUtility.HtmlEncode(templatehook.Content);
                db.Entry(templatehook).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { ParentID = templatehook.ParentID });
            }
            ViewBag.TemplateID = new SelectList(db.Templates, "ID", "Name", templatehook.TemplateID);
            ViewBag.StyleID = new SelectList(db.Styles, "ID", "Name", templatehook.StyleID);
            ViewBag.ParentID = new SelectList(db.TemplateHooks, "ID", "Name", templatehook.ParentID);
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View(templatehook);
        }

        //
        // GET: /TemplateHook/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TemplateHook templatehook = db.TemplateHooks.Find(id);
            if (templatehook == null)
            {
                return HttpNotFound();
            }
            return View(templatehook);
        }

        //
        // POST: /TemplateHook/Delete/5
        public ActionResult ajDelete(int id)
        {
            TemplateHook templatehook = db.TemplateHooks.Find(id);
            db.TemplateHooks.Remove(templatehook);
            db.SaveChanges();
            return Json(new { success = true });
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TemplateHook templatehook = db.TemplateHooks.Find(id);
            db.TemplateHooks.Remove(templatehook);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}